// pages/shopping/shopping.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		content: '',
		http: getApp().globalData.map,
		url: getApp().globalData.http+"action=shopCarIndex&userid=",
		you: 0,
		one: false,
		zt: '',
		num: 0,
		monkey: 0,
		delete1: false,
		list: '',
		id: []
	},
	onShow: function() {
		wx.getStorage({
			key: 'uid',
			success: (res) => {
				this.setData({
					uid: res.data,
					zt:'',
					num:0,
					monkey: 0,
					url: getApp().globalData.http+"action=shopCarIndex&userid=" + res.data.uid
				})
				this.qus()
			}
		})
		this.qus()

		console.log(this.data.zt)
	},
	qus() {
		let _this = this
		wx.request({
			url: this.data.url,
			success: (res) => {
				console.log(res)
				if (res.data.data.shopCar.length != 0) {
					_this.setData({
						content: res.data.data,
						you: 1
					})
				}else{
					_this.setData({
						content: res.data.data,
						you: 0,
						one: false,
						zt: '',
						num: 0,
						monkey: 0,
						delete1: false,
						list: '',
						id: []
					})
				}
				
			}
		})
	},
	dj(e) {
		let id = this.data.id
		let m = this.data.monkey
		let shu = this.data.num
		let d = this.data.content.shopCar
		let a = e.currentTarget.dataset
		if (a.zt == undefined) {
			d[a.name].Cart[a.id].zt = true
				++shu
			m += d[a.name].Cart[a.id].price * d[a.name].Cart[a.id].pnum
			console.log(d[a.name].Cart[a.id].price)
			console.log(d[a.name].Cart[a.id].pnum)
			console.log(m)
			id.push(d[a.name].Cart[a.id].ID)
			this.setData({
				id: id,
				num: shu,
				monkey: m
			})
		} else if (d[a.name].Cart[a.id].zt == true) {
			d[a.name].Cart[a.id].zt = false
				--shu
			m-=d[a.name].Cart[a.id].price * d[a.name].Cart[a.id].pnum
			for(let td in id){
				if(id[td]==d[a.name].Cart[a.id].ID){
					id.splice(td,1)
				}
			}
			this.setData({
				num: shu,
				id: id,
				monkey: m
			})
		} else if (d[a.name].Cart[a.id].zt == false) {
			d[a.name].Cart[a.id].zt = true
				++shu
			m += d[a.name].Cart[a.id].price * d[a.name].Cart[a.id].pnum
			console.log(m)
			id.push(d[a.name].Cart[a.id].ID )
			this.setData({
				num: shu,
				monkey: m,
				id:id
			})
		}
		let one = this.data.one
		for (let c in d[a.name].Cart) {
			if (d[a.name].Cart[c].zt == true) {
				d[a.name].zt = true
			} else {
				one = false
				d[a.name].zt = false
				return this.setData({
					zt: d,
					one: one
				})
			}
		}
		this.setData({
			zt: d,
			num: shu
		})
		for (let ha in d) {
			if (d[ha].zt == true) {
				one = true
			} else {
				one = false
				return this.setData({
					one: one
				})
			}
		}
		this.setData({
			one: one
		})
	},


	tnt(e) {
		let id=this.data.id
		let m = 0
		let shu = 0
		console.log(shu)
		let a = e.currentTarget.dataset.id
		let d = this.data.content.shopCar
		for (let i in d) {
			d[i].zt = !a
			for (let e in d[i].Cart) {
				if (!a) {
					++shu
					id.push(d[i].Cart[e].ID)
					m += d[i].Cart[e].price * d[i].Cart[e].pnum
				} else {
					shu=0
					for(let td in id){
						if(id[td]==d[i].Cart[e].ID){
							id.splice(td,1)
						}
					}
					m =0
				}
				d[i].Cart[e].zt = !a
			}
		}
		this.setData({
			id:id,
			one: !a,
			monkey: m,
			num: shu,
			zt: d
		})
	},

	qun(e) {
		let id=this.data.id
		let m = this.data.monkey
		let shu = this.data.num
		let a = e.currentTarget.dataset
		let d = this.data.content.shopCar
		if (a.zt == undefined) {
			d[a.id].zt = true
			let zxc = d[a.id].Cart.length
			for (let e in d[a.id].Cart) {
				if (d[a.id].Cart[e].zt == true) {
					--zxc
				} else {
					id.push(d[a.id].Cart[e].ID)
					m += d[a.id].Cart[e].price * d[a.id].Cart[e].pnum
					console.log(m)
					d[a.id].Cart[e].zt = true
				}
			}
			shu += zxc
			this.setData({
				id:id,
				zt: d,
				num: shu,
				monkey: m
			})
		} else if (d[a.id].zt == true) {
			d[a.id].zt = false
			for (let e in d[a.id].Cart) {
				d[a.id].Cart[e].zt = false
				m -= d[a.id].Cart[e].price * d[a.id].Cart[e].pnum
				for(let td in id){
					if(id[td]==d[a.id].Cart[e].ID){
						id.splice(td,1)
					}
				}
			}
			shu -= d[a.id].Cart.length
			this.setData({
				zt: d,
				id:id,
				num: shu,
				monkey: m
			})
		} else if (d[a.id].zt == false) {
			d[a.id].zt = true
			let zxc = d[a.id].Cart.length
			for (let e in d[a.id].Cart) {
				if (d[a.id].Cart[e].zt == true) {
					--zxc
				} else {
					id.push(d[a.id].Cart[e].ID)
					m += d[a.id].Cart[e].price * d[a.id].Cart[e].pnum
					d[a.id].Cart[e].zt = true
				}
			}
			shu += zxc
			this.setData({
				zt: d,
				id:id,
				num: shu,
				monkey: m
			})
		}
		let one = this.data.one
		for (let ha in d) {
			if (d[ha].zt == true) {
				one = true
			} else {
				one = false
				return this.setData({
					one: one
				})
			}
		}
		console.log(one)
		this.setData({
			one: one
		})

	},
	subtract(e) {
		console.log(e)
		let lab = e.currentTarget.dataset.id
		let b = this.data.content.shopCar[0].Cart[lab].pnum
		if (e.currentTarget.dataset.zt == 'jia') {
			b += 1
		} else {
			if (b > 1) {
				b -= 1
			}
		}
		wx.request({
			url: getApp().globalData.http + 'action=carAddorCut&shopCarID=' + this.data.content.shopCar[0].Cart[lab].ID +
				'&nums=' + b +
				'&userid=' + this.data.content.shopCar[0].Cart[lab].uid,
			success: (res) => {
				let a = this.data.content
				a.shopCar[0].Cart[lab] = res.data.data
				this.setData({
					content: a
				})
			}
		})
	},

	gb() {
		this.setData({
			delete1: !this.data.delete1
		})
	},
	dete(e) {
		if(this.data.num>0){
			let a=this.data.id
			let b=a.join(',')
			console.log(b)
			wx.showModal({
				title:'确定要删除吗',
				success:(e)=>{
					console.log(e)
					if(e.confirm){
						wx.request({
							url: getApp().globalData.http + 'action=deleteShopCar&cars=' + b +'&userid='+this.data.content.shopCar[0].Cart[0].uid,
							success:(e)=>{
								this.onShow()
							}
						})
					}
				}
			})
		}
	},
	jieshu(){
		if(this.data.num>0){
			let a=this.data.id
			let b=a.join(',')
			this.setData({
				zt:'',
				num: 0,
				one:false
			})
			if(b!=undefined&&b!=''){
				wx.navigateTo({
					url:'../shopping-place/shopping-place?cars='+b,
					success:(e)=>{
						this.setData({
							id:[]
						})
					}
				})
			}else{
				wx.showToast({
					title:'请重试',
					icon:'none'
				})
			}
		}
	}
})
